package com.xiaogao.serviceEdu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaogao.serviceBase.utils.exceptionHandler.XiaoGaoException;
import com.xiaogao.serviceEdu.client.VodClient;
import com.xiaogao.serviceEdu.entity.EduVideo;
import com.xiaogao.serviceEdu.entity.vo.VideoInfoVo;
import com.xiaogao.serviceEdu.mapper.EduVideoMapper;
import com.xiaogao.serviceEdu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author 小高
 * @since 2021-06-08
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Resource
    private VodClient vodClient;

    // chapter调用方法查询是否存在子节点
    @Override
    public boolean getChapterMenuById(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        Integer count = baseMapper.selectCount(wrapper);
        return null != count && count > 0;
    }

    @Override
    public void addVideoInfo(VideoInfoVo videoInfoVo) {
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(videoInfoVo,eduVideo);
        if (!this.save(eduVideo)){
            throw new XiaoGaoException(20001,"添加课时信息失败！");
        }
    }

    @Override
    public VideoInfoVo getVideoInfoVoById(String id) {
        // 先获取edu_video表中数据
        EduVideo eduVideo = this.getById(id);
        if (eduVideo == null){
            throw new   XiaoGaoException(20001,"数据不存在");
        }
        // 创建VideoInfoVo对象
        VideoInfoVo videoInfoVo = new VideoInfoVo();
        BeanUtils.copyProperties(eduVideo,videoInfoVo);

        return videoInfoVo;
    }

    @Override
    public void updateVideoInfoById(VideoInfoVo videoInfoVo) {
        EduVideo eduVideo = new EduVideo();
        BeanUtils.copyProperties(videoInfoVo,eduVideo);

        if (!this.updateById(eduVideo)){
            throw new XiaoGaoException(20001,"修改课时信息失败！");
        }
    }

    @Override
    public boolean removeVideo(String id) {
//        // 查询云端视频id
//        EduVideo eduVideo = baseMapper.selectById(id);
//        String videoSourceId = eduVideo.getVideoSourceId();
//        // 调用vod服务删除视频资源
//        if (!StringUtils.isEmpty(videoSourceId)){
//            vodClient.removeVideo(videoSourceId);
//        }

        Integer count = baseMapper.deleteById(id);
        return null != count && count > 0;
    }

    @Override
    public boolean removeCourseById(String courseId) {
        // 根据课程id查询所有视频列表
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.select("video_source_id");
        List<EduVideo> videoList = baseMapper.selectList(wrapper);

        // 获取所有视频列表的云端视频id
        List<String> videoSourceIdList = new ArrayList<>();
        for (int i = 0; i < videoList.size(); i++) {
            String videoSourceId = videoList.get(i).getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)){
                videoSourceIdList.add(videoSourceId);
            }
        }

        // 调用vod服务批量删除远程视频
        if (videoSourceIdList.size() > 0) {
            vodClient.removeVideoList(videoSourceIdList);
        }

        // 删除edu_video数据库表种的数据
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        Integer count = baseMapper.delete(queryWrapper);
        return null != count && count > 0;
    }
}
